key lastDetected; // small improvement

default
{
    state_entry()
    {
        llSay(0, "Collision detection now active.");
    }

    touch_start(integer total_number)
    {
        llSay(0, "Touched - recalculating llCastRay()");
        llMessageLinked(2, 999, "", NULL_KEY);
    }

    collision(integer who)
    {
        integer i;
        string names = "";
        for (i = 0; i < who; i++)
        {
            names += llDetectedName(i) + "<" + (string)llDetectedKey(i) + ">|";
        }
        if (llDetectedKey(0) == lastDetected) // small optimisation, needs improvement
        {
            llOwnerSay("Collision: We've already collided with this object »»" + llDetectedName(0) + "««");
            llSleep(1.0);
        }
        else
        {
            llOwnerSay("Collision with: " + names);
            llMessageLinked(2, 999, "", NULL_KEY); // inform our detecter that we have collided with something
            // llSleep(1.0);
            lastDetected = llDetectedKey(0);
        }
    }  
}
